package cn.bs.utils;

import cn.bs.mapper.OrderMapper;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PayUtil {
    @Autowired
    private OrderMapper orderService;
    //appid
    private final String APP_ID = "2021000147651329";
    //应用私钥
    private final String APP_PRIVATE_KEY = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCpSn0/Xbms1rxn6CD/rN3LB3vnGcKGjZWSoXjb/klm5adIV+hVnrqsWld/pbQTmgngKa+tUTy4tyClX56bH4alJ7Pg6mwzzNwzZpTUVJgu5CYRJ6OyXNtdp5uftoriU+11DOGIe5n5U1oYtWvGOeKrQ8Zlw9KQsXo/MwRC0sB8Ayfro1ssZRYhpTwOqVmL6Tk5pTZRcE9xWpwkWCW/vhHjZEenzoqV4yUdBlHrFoe8VSNi+CqXE2cGXowffhTYuSkbnjBFspp2KookoPi3nPnazFcCs2vTXXnlFRLg6vE0xdIVebHkCw7UMyOPA2/lrceW+TtTQ7kcEaRmqi8ih3hTAgMBAAECggEAWY9VyWMdKooD7HiKlJhwUcuoye7/0U0v9v3Dwaa6UFxobkZLXJh3q1yz84zHQqN4XAXRscBLfnErLTHz5577DX5+lBuQmKrlR7AaRmZmiFAcr3CoEmE7kEVueEULHodGFWl2G01AfP2SqYW4+YqAFxjo6cd6KyApSywDMhLqz+/CDOy9gi9dgJUylrnzH3rZ/lrbF3W/bwPjSv9noPt3YQUN6pxbLnH6zReqgRvP4A8R0brd9kQAS11crr0DQHKBjlha0qJBVUIJGxqp4ubkfq2/vPfLVsWTRYqc+rrB1UxTdazvKNWHFkkGDFabFCp6Mw9nzzjrFfe3DFmeZ+BgmQKBgQDmtg75Ow51kckdS3oDeu1DJ0aFQcIB7BC1tK3smcpEkz7UGH+HGMS3Vc73NN/PyDbfubOsfBaDC+7B3B3EMO8sUgl2pDuhZyQxfgtnI4CJjv4dkAGT5qYIqtYjBF2eRceEH68R5HtseuXYGt8PfT5VKW/AJNf+M2z+ifWn6KLTHwKBgQC72O47Rl6zK1NhW0UtozBcGwCmTup+1o0ckwNNiCHyib5HkBbct+vg1JHjhp+eahK3lEr0ooXvZAiNC1Qw7J8gBgRjMJ6DRxJZeCKY5iVCXYZN3bPfKBE8LMG2i/nLK1Ek2ueR/j3gCJjsZpaTu9KZkn7OKXnLNJX9vCcni2sITQKBgQCTu9UOx0BVDMuJ1MEG5Xp02/uObTM8Y9u3X0FYFoqtKtS6oA1ZhwTAmVP0fIks1no8/P8O3k7UD5oNzVPKQ8ODz+x7amM80X0drCbFHri6XTRf3c0E1ldPiYWh2rPQG8LM2jUS8goOxwvTPDHGC18UzXnfwjgFcQzEWbdSk+df5QKBgQCkNNCj3jLgOopru4Ywi8u1aCFcP/X6pIC8LsMfXlYWl1RPaGSj0t8OLaPOGhukNiNgePBBA2VDS9Wxvep9zgqXysrBDnoyVclzjnHn6J8/cVP680fOVk8ipBk+AwR8FsSbDASL+YyLL9jGJjOQtwo1FvtkKpt+B9zp7jTn+5a3rQKBgQDhQuSC+n5VibpDBZAcN6+BjiXhWoUnyRpoHigmSUuuNGMDX8uWeUHSXmsdayA6rfcbnDU++1ArAi/BRnzNQBjJmXFDC2a58KJvihOrDQWNGOYHzUgwuthrmas3gGASieK7G0EW+9qerHSpU4jG+e4Exh/kyasfUKDwnyzO4hxzNw==";
    private final String CHARSET = "UTF-8";
    // 支付宝公钥
    private final String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmQh+2s76DQBWfmlkdlwhgx3Rkf11nQOXcsfMI5tQerWKeMs9EWksiYkZKlUMd1pRdN3ksvvalHviN0UMIFDEi/xKBak71feKQlKPFqXKI132B0bFVh0vA7N0JLPQN01GFxPECb7jIRGbdm9m8IhfIjF8EXB1s6be5i7fBILb82nzJirmff9JPqtRN/J8IN1BBMRPq5FMva3pOMAPyGSdG24FbV+x+3TyYdxB1t0/8sIxIY1Nm69mc5fBvMmllPC/U38lph1sZ/q9iHmR4ngTDpJuOkXp72r4ZYQIx+Awdc4UBMqdYciwzCY8rxm+4mhlBqi2uQNDpyYnOoI9VWkGcQIDAQAB";
    //沙箱接口路径，正式路径为https://openapi.alipay.com/gateway.do
    private final String GATEWAY_URL = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    private final String FORMAT = "JSON";
    //签名方式
    private final String SIGN_TYPE = "RSA2";
    //支付宝异步通知路径,付款完毕后会异步调用本项目的方法,必须为公网地址
    private final String NOTIFY_URL = "http://2742d9.natappfree.cc/alipay/toSuccess";
    //支付宝同步通知路径,也就是当付款完毕后跳转本项目的页面,可以不是公网地址
    private final String RETURN_URL = "http://localhost:8081/alipay/toSuccess";
    private AlipayClient alipayClient = null;
    //支付宝官方提供的接口
    public String sendRequestToAlipay(String outTradeNo, Float totalAmount, String subject) throws AlipayApiException {
        System.out.println("sendRequestToAlipay。。。。。。。。。。。。。");
        //获得初始化的AlipayClient
        alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);

        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(RETURN_URL);
        alipayRequest.setNotifyUrl(NOTIFY_URL);

        //商品描述（可空）
        String body = "";
        alipayRequest.setBizContent("{\"out_trade_no\":\"" + outTradeNo + "\","
                + "\"total_amount\":\"" + totalAmount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //请求
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        System.out.println("返回的结果是："+result );
        return result;
    }

    //    通过订单编号查询
    public String query(String id){
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", id);
        request.setBizContent(bizContent.toString());
        AlipayTradeQueryResponse response = null;
        String body=null;
        try {
            response = alipayClient.execute(request);
            body = response.getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return body;
    }
}
